package test.enba.security.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enba.boot.core.base.Result;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import test.enba.security.auth.entity.UserRole;
import test.enba.security.auth.service.IUserRoleService;

@RestController
@RequestMapping("/auth/userRole")
public class UserRoleController {

  @Resource private IUserRoleService userRoleService;

  /**
   * 查询所有接口
   *
   * @return list
   */
  @GetMapping
  public Result<List<UserRole>> findAll() {
    return Result.success(userRoleService.list());
  }

  /**
   * 根据id查询数据接口
   *
   * @param id id
   * @return result
   */
  @GetMapping("/{id}")
  public Result<UserRole> findOne(@PathVariable Integer id) {
    return Result.success(userRoleService.getById(id));
  }

  /**
   * 分页查询接口
   *
   * @param pageNum nume
   * @param pageSize size
   * @return result
   */
  @GetMapping("/page")
  public Result<Page<UserRole>> findPage(
      @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
    QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
    return Result.success(userRoleService.page(new Page<>(pageNum, pageSize), queryWrapper));
  }

  /**
   * 新增和更新接口
   *
   * @param userRole userRole
   * @return result
   */
  @PostMapping
  public Result<Boolean> save(@RequestBody UserRole userRole) {
    return Result.success(userRoleService.saveOrUpdate(userRole));
  }

  /**
   * 删除接口
   *
   * @param id id
   * @return result
   */
  @DeleteMapping("/{id}")
  public Result<Boolean> delete(@PathVariable Integer id) {
    return Result.success(userRoleService.removeById(id));
  }

  /**
   * 批量删除接口
   *
   * @param ids ids
   * @return result
   */
  @PostMapping("/del/batch")
  public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
    return Result.success(userRoleService.removeByIds(ids));
  }
}
